﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Web;

namespace XMK.Utils
{
    public class WebHelper
    {
        /// <summary>
        /// 获取浏览器版本号 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static string GetBrowser(HttpContext httpContext)
        {
            return GetBrowser(httpContext.Request);
        }
        public static string GetBrowser(HttpRequest httpRequest)
        {
            HttpBrowserCapabilities bc = httpRequest.Browser;
            string aa = bc.Browser.ToString();
            string bb = bc.Version.ToString();
            return aa + bb;
        }

        /// <summary>
        /// 获取操作系统版本号   
        /// </summary>
        /// <returns></returns>
        public static string SystemCheck(HttpRequest httpRequest)
        {
            string Agent = httpRequest.ServerVariables["HTTP_USER_AGENT"];
            if (string.IsNullOrEmpty(Agent))
                Agent = string.Empty;

            if (Agent.IndexOf("NT 4.0") > 0)
            {
                return "Windows NT ";
            }
            else if (Agent.IndexOf("NT 5.0") > 0)
            {
                return "Windows 2000";
            }
            else if (Agent.IndexOf("NT 5.1") > 0)
            {
                return "Windows XP";
            }
            else if (Agent.IndexOf("NT 5.2") > 0)
            {
                return "Windows 2003";
            }
            else if (Agent.IndexOf("NT 6.0") > 0)
            {
                return "Windows Vista";
            }
            else if (Agent.IndexOf("WindowsCE") > 0)
            {
                return "Windows CE";
            }
            else if (Agent.IndexOf("NT") > 0)
            {
                return "Windows NT ";
            }
            else if (Agent.IndexOf("9x") > 0)
            {
                return "Windows ME";
            }
            else if (Agent.IndexOf("98") > 0)
            {
                return "Windows 98";
            }
            else if (Agent.IndexOf("95") > 0)
            {
                return "Windows 95";
            }
            else if (Agent.IndexOf("Win32") > 0)
            {
                return "Win32";
            }
            else if (Agent.IndexOf("Linux") > 0)
            {
                return "Linux";
            }
            else if (Agent.IndexOf("SunOS") > 0)
            {
                return "SunOS";
            }
            else if (Agent.IndexOf("Mac") > 0)
            {
                return "Mac";
            }
            else if (Agent.IndexOf("Linux") > 0)
            {
                return "Linux";
            }
            else if (Agent.IndexOf("Windows") > 0)
            {
                return "Windows";
            }
            return "未知类型";
        }

        public static string SystemCheck(HttpContext httpContext)
        {
            return SystemCheck(httpContext.Request);
        }
        /// <summary>
        /// 获取客户端IP地址  
        /// </summary>
        /// <returns></returns>
        public static string GetIP(HttpRequest httpRequest)
        {
            string result = String.Empty;
            result = httpRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            if (null == result || result == String.Empty)
            {
                return "0.0.0.0";
            }
            return result;
        }
        public static string GetIP(HttpContext httpContext)
        {
            return GetIP(httpContext.Request);
        }
        /// <summary>
        /// 取本系统IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP()
        {
            ManagementClass managecls = new ManagementClass("Win32_NetworkAdapterConfiguration");
            foreach (ManagementObject manageobj in managecls.GetInstances())
            {
                if ((bool)manageobj["IPEnabled"])
                {
                    string[] strArray = (string[])manageobj["IPAddress"];
                    if (strArray.Length > 0)
                    {
                        return strArray[0];
                    }
                }
            }
            return string.Empty;
        }
    }
}
